package cn.sdc.bugKiller.service;

import java.util.HashMap;
import java.util.List;

import cn.sdc.bugKiller.bean.BugCategroyBean;
import cn.sdc.bugKiller.bean.CodeBean;
import cn.sdc.bugKiller.bean.MemberBean;
import cn.sdc.bugKiller.bean.ModuleBean;
import cn.sdc.bugKiller.bean.ProgramBean;
import cn.sdc.bugKiller.bean.ProjectBean;
import cn.sdc.bugKiller.dao.CodeDaoImpl;
import cn.sdc.bugKiller.dao.MemberDaoImpl;

public class CodeDealService {

	private static CodeDaoImpl codeDao = new CodeDaoImpl();
	private static MemberDaoImpl memberDao = new MemberDaoImpl();
	
	public List<CodeBean> getCodeBeanList(String codeTblName, Class beanClass){
		return codeDao.getAllCode(codeTblName, beanClass);
	}
	
	public HashMap<Integer, String> getCodeMap(String codeTblName){
		HashMap<Integer, String> codeMap = null;
		List<CodeBean> beanList = codeDao.getAllCode(codeTblName);
		if(beanList != null && !beanList.isEmpty()){
			codeMap = new HashMap<Integer, String>();
			for(CodeBean bean : beanList){
				codeMap.put(bean.getId(), bean.getName());
			}
		}
		return codeMap;
	}
	
	public HashMap<Integer, String> getModuleMap(){
		return getCodeMap(ModuleBean.TBL_NAME);
	}
	
	public HashMap<Integer, String> getProgramMap(){
		return getCodeMap(ProgramBean.TBL_NAME);
	}
	
	public HashMap<Integer, String> getBugCategoryMap(){
		return getCodeMap(BugCategroyBean.TBL_NAME);
	}
	
	public HashMap<Integer, String> getMemberMap(){
		
		HashMap<Integer, String> memberMap = null;
		List<MemberBean> beanList = memberDao.selectAllUser();
		if(beanList != null && !beanList.isEmpty()){
			memberMap = new HashMap<Integer, String>();
			for(MemberBean bean : beanList){
				memberMap.put(bean.getId(), bean.getUsername());
			}
		}
		return memberMap;
	}
	
	
	public ProjectBean getProjectById(Integer projectId){
		if(projectId == null){
			//default project;
			projectId = ProjectBean.DEFAULT_PROJECT;
		}
		CodeBean bean = (CodeBean)codeDao.getCodeById(projectId, ProjectBean.TBL_NAME, ProjectBean.class);
		return ProjectBean.clone(bean);
//		return ProjectBean.clone(codeDao.getCodeById(projectId, ProjectBean.TBL_NAME));
	}
}
